package ru.ifmo.rybakov.muclumon;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RevisionFactory {
	
	private static final Logger logger = LoggerFactory.getLogger(RevisionFactory.class);
	
	public static Revision createRevision(RevisionType type) {
		Revision revision = new Revision();
		revision.setType(type);
		
		EntityManager em = Database.getEntityManagerFactory().createEntityManager();
		try {
			EntityTransaction t = em.getTransaction();
			try {
				t.begin();
				em.persist(revision);
				t.commit();
			} finally {
				if (t.isActive()) {
					logger.warn("Rolling back transaction");
					t.rollback();
				}
			}
		} finally {
			em.close();
		}
		return revision;
	}
}
